﻿package rmhc.gui
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class ScrollCalc// extends EventDispatcher
	{
		protected var min:Number , max:Number;
		protected var q:Number;
		//protected var targ:DisplayObject;
		
		public function ScrollCalc( min:Number = Number.MIN_VALUE , max:Number = Number.MAX_VALUE , q:Number = 0 )
		{
			
			this.q = q;
			
			this.min = min;
			this.max = max;
		}
		public function set minimum( n:Number ):void
		{
			//min + ( max - min ) * q1 = n + ( max - n ) * q;
			//q = ( min + ( max - min ) * q1 - n ) / ( max - n )
			
			//q = ( min + ( max - min ) * q - n ) / ( max - n );
			
			min = n;
		}
		public function get minimum():Number
		{
			return min;
		}
		
		public function set maximum( n:Number ):void
		{
			//min + ( max - min ) * q = min + ( n - min ) * qn;
			//qn = ( max - min ) * q / ( n - min )
			
			//q = ( max - min ) * q / ( n - min );
			
			max = n;
		}
		public function get maximum():Number
		{
			return max;
		}
		
		public function set pos( p:Number ):void
		{
			if ( p >= 1 )
			{
				q = 1;
			}
			else if ( p <= 0 )
			{
				q = 0;
			}
			else
			{
				q = p;
			}
			
			//dispatchEvent( new Event( Event.CHANGE ) );
		}
		public function get pos():Number
		{
			return q;
		}
		
		public function get value():Number
		{
			return min + ( max - min ) * q;
		}
		public function set value( c:Number ):void
		{
			q = ( c - min ) / ( max - min );
			if ( q > 1 )
			{
				q = 1;
			}
			else if ( q < 0 )
			{
				q = 0;
			}
			
			//Stamper.debug( "chg: " + c + "/" + max + " , " + q + "/1" );
			//dispatchEvent( new Event( Event.CHANGE ) );
		}
	}
}